package com.aispeech.task.lasr.event;

import com.aispeech.task.common.data.domain.Task;
import com.aispeech.task.common.exceptions.TaskException;
import com.aispeech.task.constant.LASRProgress;
import com.aispeech.task.fsm.Event;
import com.aispeech.task.lasr.LASREventType;
import lombok.extern.slf4j.Slf4j;

import static com.aispeech.task.lasr.LASRTaskHandler.updateTaskStatus;

@Slf4j
public class AudioCutEvent extends Event<Task> {
    public AudioCutEvent(Task task) {
        super(LASREventType.ChannelSplitted, task);
    }

    public AudioCutEvent(LASREventType eventType, Task task) {
        super(eventType, task);
    }

    @Override
    public Event fire() {
        Task task = this.getTarget();

        Event nextEvent;

        try {
            updateTaskStatus(task, LASRProgress.AUDIOCUT, "ts_audiocut");
            Thread.sleep(1000L);
            nextEvent = new AsrEvent(task);
            updateTaskStatus(task, LASRProgress.AUDIOCUTTED, "ts_audiocutted");
        } catch (InterruptedException | TaskException ex) {
            nextEvent = new ErrorEvent(task);
        }

        return nextEvent;
    }
}
